package jinyilw.swing.visuals.Icomp;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.icon.LazyScaleIcon;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.Icon;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.io.File;
import java.net.URL;

public class IRichLabel extends ILabel
{
	private static final long serialVersionUID = 8425186977448479454L;
	private String text;
	private int hy;
	private int textW, textH;
	private float leftX = 0, leftY = 0, rightX = 1, rightY = 1;
	private Color leftColor, rightColor;
	private boolean autoShadow = true;
	private final RenderingConfig renderingConfig = new RenderingConfig();

	public IRichLabel()
	{
		super.setFont(FontTools.defaultFont);
		setForeground(Color.cyan);
	}

	public IRichLabel(String text)
	{
		super.setFont(FontTools.defaultFont);
		setForeground(Color.cyan);
		setText(text);
	}

	public IRichLabel(String text, int alignment)
	{
		super.setFont(FontTools.defaultFont);
		setForeground(Color.cyan);
		setHorizontalAlignment(alignment);
		setText(text);
	}

	public IRichLabel(String text, Font font, int alignment)
	{
		super.setFont(font);
		setForeground(Color.cyan);
		setHorizontalAlignment(alignment);
		setText(text);
	}

	public IRichLabel(Font font, Color c)
	{
		setForeground(c);
		super.setFont(font);
	}

	public IRichLabel(String text, Font font, Color c)
	{
		setForeground(c);
		super.setFont(font);
		setText(text);
	}

	public IRichLabel(ICSetting iSetting)
	{
		super.setFont(FontTools.defaultFont);
		setText(iSetting);
	}

	public IRichLabel(ICSetting iSetting, int alignment)
	{
		setHorizontalAlignment(alignment);
		super.setFont(FontTools.defaultFont);
		setText(iSetting);
	}

	@Override
	public void updateUI()
	{
		// setUI(new BasicLabelUI());
	}

	public void setLeftShadow(float x, float y, Color color)
	{
		leftX = x;
		leftY = y;
		leftColor = color;
	}

	public void setRightShadow(float x, float y, Color color)
	{
		rightX = x;
		rightY = y;
		rightColor = color;
		autoShadow = false;
	}

	public void setAutoShadow(boolean b)
	{
		autoShadow = b;
	}

	public void setParameter(String text, Font font, Color color)
	{
		super.setFont(font);
		setForeground(color);
		setText(text);
	}

	public void setParameter(ICSetting iSetting, Font font, Color color)
	{
		super.setFont(font);
		setForeground(color);
		setText(iSetting);
	}

	@Override
	public void setText(String text)
	{
		if (text == null || text.equals(this.text))
			return;
		this.text = text;
		updateTSize(getFont());
		revalidate();
		repaint();
	}

	@Override
	public String getText()
	{
		return text;
	}

	@Override
	public void setFont(Font font)
	{
		if (font == null || font.equals(getFont()))
			return;
		if (autoShadow)
		{
			float fontSize = font.getSize();
			if (fontSize > 12)
				rightY = rightX = fontSize / 15;
			else
				rightY = rightX = 0.8f;
		}
		updateTSize(font);
		super.setFont(font);
	}

	@Override
	public Dimension getPreferredSize()
	{
		return new Dimension(textW + 1, textH);
	}

	//	public void setIcon(String name)
	//	{
	//		setIcon(LazyScaleIcon.getIcon(name));
	//	}

	public void setIcon(URL url)
	{
		setIcon(LazyScaleIcon.getIcon(url));
	}

	public void setIcon(File file)
	{
		setIcon(LazyScaleIcon.getIcon(file));
	}

	private void updateTSize(Font font)
	{
		if (text == null || font == null)
			return;
		FontMetrics fm = getFontMetrics(font);
		hy = fm.getAscent();
		textW = fm.stringWidth(text);
		textW += leftX + rightX;
		textH = fm.getHeight();
		textH += leftY + rightY;
	}

	@Override
	protected void paintComponent(Graphics g)
	{
		if (text == null || text.isEmpty())
			return;
		Graphics2D g2d = (Graphics2D) g;
		Composite composite = ComponentTools.applyEnableAlpha(g2d, isEnabled(),
				1f, 0.3f);
		Icon icon = getIcon();
		Insets insets = getInsets();
		int width = getWidth() - insets.left - insets.right;
		int height = getHeight() - insets.top - insets.bottom;
		int iX = insets.left, iY = insets.top, iW = 0, gap = 0;
		if (icon != null)
		{
			iY += (height - icon.getIconHeight()) / 2;
			iW = icon.getIconWidth();
			gap = getIconTextGap();
		}
		int tX, tY = (height - textH) / 2 + hy;
		switch (getHorizontalAlignment())
		{
			case CENTER:
				iX += (width - iW - gap - textW) / 2;
				tX = iX + iW + gap;
				break;
			case RIGHT:
				iX += width - iW - gap - textW - 1;
				tX = iX + iW + gap;
				break;
			default:
				iX += 1;
				tX = iX + iW + gap;
		}

		if (icon != null)
			icon.paintIcon(this, g2d, iX, iY);

		renderingConfig.setTextAADrawing(g2d);
		if (leftColor != null)
		{
			g.setColor(leftColor);
			g2d.translate(-leftX, -leftY);
			g.drawString(text, tX, tY);
			g2d.translate(leftX, leftY);
		}
		if (rightColor != null)
			g.setColor(rightColor);
		else
			g.setColor(RThemeColor.colorSet.shadowColor);
		g2d.translate(rightX, rightY);
		g.drawString(text, tX, tY);
		g2d.translate(-rightX, -rightY);
		g.setColor(getForeground());
		g.drawString(text, tX, tY);
		renderingConfig.resetTextAADrawing(g2d);
		if (composite != null)
			g2d.setComposite(composite);
	}
}
